
;
(function($, $$) {
    $.fn.progressBar = function(options) {
        var self = this, args = Array.prototype.slice.call(arguments);
        args.push($$.ProgressBar);
        var result = $$.createWidgetByJQuery.apply(self, args);
        if (result.jqObj !== undefined && options !== "widget") {
            return self;
        }
        return result;
    };

    $$.ProgressBar = function(self, options) {
        if (!(this instanceof $$.ProgressBar)) {
            return new $$.ProgressBar(self, options);
        }
        var defaults = {

            progressBarClass : $$.defaultGradientBG + " " + $$.defaultBorder,

            progressBlockClass : $$.hoverGradientBG,

            blockWidth : 12,

            marginValue : null,

            type : "normal",

            scrollTime : 3000,

            scrollValue : 0.19,


            value : 0,

            progressBarLength : 0,

            scrolling : false
        };
        var opts = $.extend(defaults, options);
        $$.ProgressBar.prototype.superClass.call(this, self, opts);
        this.init();
    }
    $$.inherit($$.ProgressBar, $$.Widget, {

        init : function() {
            var progressBarObj = this, opts = progressBarObj.opts;
            progressBarObj.createWidget();
            initProgressBar(progressBarObj.jqObj, opts);

            if (opts.type === "scroll") {
                progressBarObj.val(opts.scrollValue);
                progressBarObj.scroll(true);
            }
            else {
                progressBarObj.val(opts.scrollValue);
            }
            return progressBarObj;
        },

        val : function(value) {
            var progressBar = this, self = progressBar.jqObj, opts = progressBar.opts;
            if (value === undefined) {
                return opts.value;
            }
            opts.value = value > 1 ? 1 : (value < 0 ? 0 : value);
            value = opts.value === 1 ? 1.1 : opts.value;
            self.children(".progressValue").width(opts.progressBarLength * value);
            return progressBar;
        },

        scroll : function(scrolling) {
            var progressBarObj = this, self = progressBarObj.jqObj, opts = progressBarObj.opts;
            if (opts.type !== "scroll" || (scrolling && opts.scrolling)) {
                return progressBarObj;
            }
            if (!scrolling) {
                opts.scrolling = false;
                self.children(".progressValue").stop();
            }
            else {
                var obj = self.children(".progressValue");
                var marginLeftValue = opts.progressBarLength - (obj.width() + parseInt(obj.css("margin-left")));
                opts.scrolling = true;
                obj.animate({marginLeft : marginLeftValue}, opts.scrollTime, function() {
                    opts.scrolling = false;
                    progressBarObj.scroll(self, opts, true);
                });
            }
            return progressBarObj;
        }
    });

    function initProgressBar(self, opts) {
        var progressValue = $('<div class="progressValue"></div>'), marginValue = opts.marginValue || 2;
        var blockTotal = Math.ceil(self.width() / (opts.blockWidth + marginValue));
        for (var i = 0; i < blockTotal; i++) {
            var obj = $('<div class="progressBlock"></div>').addClass(opts.progressBlockClass).width(opts.blockWidth);
            if (opts.marginValue !== null) {
                obj.css("marginRight", opts.marginValue);
            }
            progressValue.append(obj);
        }
        opts.progressBarLength = self.addClass("uiProgressBar uiWidget " + opts.progressBarClass).append(progressValue).width();
        if (opts.disabled) {
        }
    }
})(jQuery, BASE);